package com.beidouin.sensortest;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.friendlyarm.AndroidSDK.GPIOEnum;
import com.friendlyarm.AndroidSDK.HardwareControler;

public class UltrasonicActivity extends AppCompatActivity implements Runnable,View.OnClickListener ,RadioGroup.OnCheckedChangeListener{

    private String devName = "dev/ttyAMA2";
    private int speed = 9600;
    private int dataBits = 8;
    private int stopBits = 1;
    private int devfd = -1;

    TextView ulDis_txv,ulTem_txv;
    RadioGroup ulSta_group;
    Button diatance_button;
    int ulSta;

    Thread t = new Thread(this);
    Handler handler;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ultrasonic);
        ulDis_txv = (TextView) findViewById(R.id.uldistance);
        ulTem_txv= (TextView) findViewById(R.id.ultemperature);

        ulSta_group= (RadioGroup) findViewById(R.id.ultra_sta);
        diatance_button= (Button) findViewById(R.id.ulstart_btn);
        ulSta_group.setOnCheckedChangeListener(this);
        diatance_button.setOnClickListener(this);


        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x101) {
                    int distance=msg.getData().getInt("dis");
                    ulDis_txv.setText(distance+"mm");
                    if(distance>4500)
                        ulDis_txv.setText("超出测量范围");
                    int temperature=msg.getData().getInt("tem");
                    ulTem_txv.setText(temperature+"℃");
                    if(temperature>70||temperature<-20)
                        ulTem_txv.setText("超出测量范围");
                }
                super.handleMessage(msg);
            }
        };
        t.start();
    }


    @Override
    protected void onDestroy() {
        //将线程销毁掉
        t.interrupt();
        HardwareControler.close(devfd);
        super.onDestroy();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ulstart_btn:
            {
                devfd = HardwareControler.openSerialPort( devName, speed, dataBits, stopBits );
                byte[] data1;
                byte[] buf1=new byte[2];
                data1=new byte[1];
                data1[0]=0x55;
                HardwareControler.write(devfd, data1);
                HardwareControler.select(devfd,0,500000);
                HardwareControler.read(devfd,buf1,2);
                int distance=buf1[0]>0?(buf1[0]*256+buf1[1])&0x0FFFF:buf1[1]&0x0FF;
                ulDis_txv.setText(distance+"mm");
                if(distance>4500)
                    ulDis_txv.setText("超出测量范围");

                byte[] data2;
                byte[] buf2=new byte[1];
                data2=new byte[1];
                data2[0]=0x50;
                HardwareControler.write(devfd, data2);
                HardwareControler.select(devfd,0,500000);
                HardwareControler.read(devfd,buf2,1);
                int temperature=buf2[0]-45;
                ulTem_txv.setText(temperature+"℃");
                if(temperature>70||temperature<-20)
                    ulTem_txv.setText("超出测量范围");
                HardwareControler.close(devfd);
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (ulSta_group.getCheckedRadioButtonId())
        {
            case R.id.ulByHand:
            {
                ulSta=1;
                diatance_button.setClickable(true);
                diatance_button.setVisibility(Button.VISIBLE);
            }
            break;
            case R.id.ulByComp:
            {
                ulSta=2;
                diatance_button.setClickable(false);
                diatance_button.setVisibility(Button.INVISIBLE);
            }
            break;
        }
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted())
        {
            Message m=handler.obtainMessage();
            int uldistance_t;
            int ultemperature_t;
            switch (ulSta)
            {
                case 2:
                {
                    devfd = HardwareControler.openSerialPort( devName, speed, dataBits, stopBits );
                    byte[] data1;
                    byte[] buf1=new byte[2];
                    data1=new byte[1];
                    data1[0]=0x55;
                    HardwareControler.write(devfd, data1);
                    HardwareControler.select(devfd,0,500000);
                    HardwareControler.read(devfd,buf1,2);
                    uldistance_t=buf1[0]>0?(buf1[0]*256+buf1[1])&0x0FFFF:buf1[1]&0x0FF;

                    byte[] data2;
                    byte[] buf2=new byte[1];
                    data2=new byte[1];
                    data2[0]=0x50;
                    HardwareControler.write(devfd, data2);
                    HardwareControler.select(devfd,0,500000);
                    HardwareControler.read(devfd,buf2,1);
                    ultemperature_t=buf2[0]-45;
                    HardwareControler.close(devfd);

                    m.what=0x101;
                    Bundle bundle =new Bundle();
                    bundle.putInt("tem", ultemperature_t);
                    bundle.putInt("dis", uldistance_t);
                    m.setData(bundle);
                    handler.sendMessage(m);
                }
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}